/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package monitoring.mapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import monitoring.beans.XLS.MonitoringXLS;
import monitoring.beans.database.MonitoringDB;
import monitoring.beans.util.ImportMessage;

/**
 *
 * @author allankv
 */
public class XLStoDB {

    private List<ImportMessage> msgs;

    public XLStoDB() {
        this.msgs = new ArrayList<ImportMessage>();
    }

    public MonitoringDB mapping(MonitoringXLS x, int row) {
        MonitoringDB db = new MonitoringDB();
        db.setRow(row);        
        db.getRecordLevelElement().getBasisOfRecord().setBasisOfRecord("Preserved Specimen");
        db.getLocalityElement().setVerbatimElevation(x.getAltitude() == null ? "" : x.getAltitude().toString());
        db.setAmostralNumber(x.getAmostralNumber());
        db.getOcurrenceElement().setCatalogNumber(x.getCatalogNumber());
        db.getLocalityElement().getMunicipality().setMunicipality(x.getCity());
        db.getRecordLevelElement().getCollectionCode().setCollectionCode(x.getCollectionCode());
        db.setCollectDate(x.getCollectionDate());        
        db.getTechnicalCollection().setTechinicalCollection(x.getCollectionTechinique());
        db.setCollectTime(x.getCollectionTime());
        db.getCollector().setCollector(x.getCollector());
        db.getLocalityElement().setLocationRemark(x.getComplementaryDataOfLocation());

        db.getLocalityElement().setCoordinatePrecision(x.getCoordinatePrecision());
        db.getLocalityElement().getCountry().setCountry(x.getCountry());
        db.getCultivar().setCultivar(x.getCultivate());
        db.getCulture().setCulture(x.getCulture());
        db.getDenomination().setDenomination(x.getDenomination());

        db.getTaxonomicElement().getFamily().setFamily(x.getFamily());
        db.setFloorheight(x.getFloorHeight());
        db.getTaxonomicElement().getGenus().setGenus(x.getGenus());
        db.getGeospatialElement().setGeodeticDatum(x.getGeodeticDatum());
        db.getGeospatialElement().setReferencePoints(x.getGpsReferencePoints());
        db.setIdGeral(x.getIdgeral());
        db.setInstallationDate(x.getInstalationDate());
        db.setInstallationTime(x.getInstalationTime());
        db.getRecordLevelElement().getInstitutionCode().setInstitutionCode(x.getInstitutionCode());
        db.getGeospatialElement().setDecimalLatitude(x.getLatitude());
        if (!x.getLcaDimensions().trim().equals("")){
            String[] lca = x.getLcaDimensions().split("/");
            if (lca.length == 3) {
                try {
                    db.setWidth(BigDecimal.valueOf(Double.parseDouble(lca[0].replaceAll(",", "."))));
                } catch (Exception e) {
                    ImportMessage msg = new ImportMessage(ImportMessage.TYPE_WARNING, "Formato numérico incorreto", ImportMessage.IMPL_FIELD_OMIT, row, "Largura (LCA)");
                    msgs.add(msg);
                    db.setWidth(null);
                }
                try {
                    db.setLength(BigDecimal.valueOf(Double.parseDouble(lca[1].replaceAll(",", "."))));
                } catch (Exception e) {
                    ImportMessage msg = new ImportMessage(ImportMessage.TYPE_WARNING, "Formato numérico incorreto", ImportMessage.IMPL_FIELD_OMIT, row, "Comprimento (LCA)");
                    msgs.add(msg);
                    db.setLength(null);
                }
                try {
                    db.setHeight(BigDecimal.valueOf(Double.parseDouble(lca[2].replaceAll(",", "."))));
                } catch (Exception e) {
                    ImportMessage msg = new ImportMessage(ImportMessage.TYPE_WARNING, "Formato numérico incorreto", ImportMessage.IMPL_FIELD_OMIT, row, "Altura (LCA)");
                    msgs.add(msg);
                    db.setHeight(null);
                }
            } else {
                ImportMessage msg = new ImportMessage(ImportMessage.TYPE_WARNING, "Formato LCA (largura, comprimento e altura) incorreto (Ex.: 10,0/300,0/8,3).", ImportMessage.IMPL_FIELD_OMIT, row, "Dimensões LCA");
                msgs.add(msg);
                db.setWidth(null);
                db.setLength(null);
                db.setHeight(null);
            }
        }
        db.getLocalityElement().getLocality().setLocality(x.getLocation());
        db.getGeospatialElement().setDecimalLongitude(x.getLongitude());
        db.getTaxonomicElement().getOrder().setOrder(x.getOrder());
        db.getColorPanTrap().setColorPanTrap(x.getPanTrapColor());
        db.setPlotNumber(x.getPlotNumber());
        db.getTaxonomicElement().setVernacularName(x.getPopularName());
        db.getPredominantBiome().setPredominantBiome(x.getPredominatBiome());
        db.getTaxonomicElement().getScientificName().setScientificName(x.getScientificName());
        db.getOcurrenceElement().getSex().setSex(x.getSex());
        db.getSupportType().setSupportType(x.getSupporttype());
        db.getTaxonomicElement().getSpeciesName().setSpeciesName(x.getSpecies());
        db.getLocalityElement().getStateProvince().setStateProvince(x.getState());
        db.getTaxonomicElement().getSubSpecies().setSubSpecies(x.getSubSpecies());
        db.getTaxonomicElement().getSubTribe().setSubTribe(x.getSubTribe());
        db.getSurroundingsCulture().setSurroundingsCulture(x.getSurroundingsCulture());
        db.getSurroundingsVegetation().setSurroundingsVegetation(x.getSurroundingsVegetation());
        db.getTaxonomicElement().getTribe().setTribe(x.getTribe());
        db.setWeight(x.getWeight());
        db.getDigitizer().setDigitizer(x.getDigitizer());
        db.getLocalityElement().setCoordinatePrecision(x.getGpsPrecision());

        return db;
    }

    public List<ImportMessage> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<ImportMessage> msgs) {
        this.msgs = msgs;
    }
}
